import fs from 'fs';
import Config from './Config.js';
import path from 'path';
import { AppName } from '../app.config.js';
/*
  数据封装
 */
class StarrySkyData {
	constructor() {
    		//获取配置文件参数
    		this.configData = Config.getConfig('version', 'version');
		
    		//文件路径参数
    		//插件根目录
    		const __dirname =
      		path.resolve() + path.sep + 'plugins' + path.sep + AppName;
    		this.filePathMap = {
			 //用户数据
      			player: path.join(__dirname, '/resource/data/player'),
			//境界
      			level: path.join(__dirname, '/resource/data/level'),
   	 	};
    		this.lib_path = this.filePathMap.lib;
    		this.givename_path=this.filePathMap.givename;
    		this.timelimit = this.filePathMap.timelimit;
    		this.level = this.filePathMap.level;
    		this.Occupation = this.filePathMap.occupation;
    		//基因境界
    		this.alevel_list = JSON.parse(fs.readFileSync(`${this.level}/基因境界.json`));
    		//灵魂境界
   	 	this.blevel_list = JSON.parse(fs.readFileSync(`${this.level}/灵魂境界.json`));
   	 	//原能境界
   	 	this.clevel_list = JSON.parse(fs.readFileSync(`${this.level}/原能境界.json`));
   	 	//法则境界
   	 	this.dlevel_list = JSON.parse(fs.readFileSync(`${this.level}/法则境界.json`));
   	 	//星空境界
   	 	this.elevel_list = JSON.parse(fs.readFileSync(`${this.level}/星空境界.json`));
	}

/**
* 检测存档存在
* @param file_path_type ["player" , "association" ]
* @param file_name
*/
existData(file_path_type, file_name)
{
    	let file_path;
    	file_path = this.filePathMap[file_path_type];
    	let dir = path.join(file_path + '/' + file_name + '.json');
    	if (fs.existsSync(dir))
	{
      		return true;
    	}
    		return false;
 }
/**
* 
* @param list  json文件名
* @param path  路径
*/
getlist(list,path)
{
	try{
            		const filePath = `${path}/${list}`;
            		const data = fs.readFileSync(filePath, 'utf8');
            		return JSON.parse(data);
          	} catch (error) {
            		console.error('读取文件错误：' + error);
            		return 'error';
          	}
}
/**
* 
* @param list  json文件名
* @param path  路径
* @param data  getlist函数
*/
writelist(list, path, data)
{
    	try {
    		const filePath = `${path}/${list}`;
    		const jsonData = JSON.stringify(data);
    		fs.writeFileSync(filePath, jsonData, 'utf8', err => {
      			console.log('写入成功', err);
    		});
            	} catch (error) {
              		console.error('写入文件错误：' + error);
            	}
}

/**
* 获取文件数据(user_qq为空查询item下的file_name文件)
* @param file_name  [player,equipment,najie]
* @param user_qq
*/
getData(file_name, user_qq)
{
	let file_path;
    	let dir;
    	let data;
    	if (user_qq) 
	{	
      		//带user_qq的查询数据文件
      		file_path = this.filePathMap[file_name];
      		dir = path.join(file_path + '/' + user_qq + '.json');
    	} else {
      		//不带参数的查询item下文件
      		file_path = this.filePathMap.lib;
      		dir = path.join(file_path + '/' + file_name + '.json');
    	}
    	try {
      		data = fs.readFileSync(dir, 'utf8');
    	} catch (error) {
      		logger.error('读取文件错误：' + error);
      		return 'error';
    	}
    	//将字符串数据转变成json格式
    	data = JSON.parse(data);
    	return data;
}

/**
* 写入数据
* @param file_name [player,equipment,najie]
* @param user_qq
* @param data
*/
setData(file_name, user_qq, data) 
{
    	let file_path;
    	let dir;
    	if (user_qq) 
	{
     		 file_path = this.filePathMap[file_name];
     		 dir = path.join(file_path + '/' + user_qq + '.json');
    	} else {
      		file_path = this.filePathMap.lib;
      		dir = path.join(file_path + '/' + file_name + '.json');
    	}
    	let new_ARR = JSON.stringify(data, '', '\t'); //json转string
    	if (fs.existsSync(dir)) 
	{
      		fs.writeFileSync(dir, new_ARR, 'utf-8', err => {
        			console.log('写入成功', err);
      		});
    	}
    	return;
}





}

export default new StarrySkyData();
